$(function(){
	//登录页
	$(".register").hide();
	$("#login").bind("click",function(){
		$(this).addClass("click");
		$(this).siblings().removeClass("click");
		$(".login").show();
		$(".register").hide();
		$(".box_button").text("登录");
		$("#opetype").val(0);
	});
	$("#register").bind("click",function(){
		$(this).addClass("click");
		$(this).siblings().removeClass("click");
		$(".login").hide();
		$(".register").show();
		$(".box_button").text("注册");
		$("#opetype").val(1);
	});
	//发布页
	$("#nav span").bind("click",function(){
		$(this).addClass("span_click").siblings().removeClass("span_click");
		var colId = $(this).attr("id");
		$("#COLUMN_ID").val(colId);
	});
	$("#icon span").bind("click",function(){
		var len = $("#icon .span_click").length;
		if($(this).hasClass("span_click")){
			$(this).removeClass("span_click");//移除lable
		}else{
			if(len>4){
				alert("标签最多选择5个");
			}else{
				$(this).addClass("span_click");
			}
		}
	})	;
	
	$(".content_list").bind('DOMNodeInserted', function(e) {
//	    alert('element now contains: ' + $(e.target).html());
		$(".del_but").bind("click",function(){
				var that = $(this);
				var indexhtml = that.parent().html();
				
				if($("#screen_pic .screen_cut").length < 4){
					that.parent().remove();
				}else{
					if($("#screen_pic .del_but").length>3){
						var path = " ";
						that.siblings("img").attr("src",path);
						that.remove();
					}else{
						that.parent().remove();
					}
					
				}
			});
		});
	$("#sumbit_but").click(function(){
		var len = $("#screen_pic .screen_cut .del_but").length;
		$("#screen_pic .screen_cut").children("input[type=file]").removeAttr("name");
		for (var i=0;i<len;i++) {
			if($("#screen_pic .screen_cut div").hasClass("del_but")){
				$("#screen_pic .screen_cut .del_but").eq(i).siblings("input[type=file]").attr("name","img"+i);
			}else{
			}	
		}
	});
});



//下面用于图片上传预览功能
function setImagePreview(index) {
	var docObj=document.getElementById("hdnImg" + index);
	var imgObjPreview=document.getElementById("imgPreview" + index);
	if(docObj.files &&docObj.files[0]){
		//火狐下，直接设img属性
		imgObjPreview.style.display = 'block';
		//imgObjPreview.src = docObj.files[0].getAsDataURL();
		 
		//火狐7以上版本不能用上面的getAsDataURL()方式获取，需要一下方式
		var imgFile = docObj.files[0];
		var filesize = imgFile.size;
		if(filesize > 5*1024*1024){
			alert("图片不能大于5M！");
			return;
		}
		imgObjPreview.src = window.URL.createObjectURL(imgFile);
	}else{
		//IE下，使用滤镜
		docObj.select();
		var imgSrc = document.selection.createRange().text;
		var localImagId = document.getElementById("localImag");
		//必须设置初始大小
		//图片异常的捕捉，防止用户修改后缀来伪造图片
		try{
		localImagId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
		localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
		}
		catch(e)
		{
		alert("您上传的图片格式不正确，请重新选择!");
		return false;
		}
		imgObjPreview.style.display = 'none';
		document.selection.empty();
	}
//	alert(index);
	if($("#pic"+index).children("div").hasClass("del_but")){
		
	}else{
		var i =index
		index++
		if($(".screen_cut").length<4){
			$("#pic"+i).parent().append("<div class='input_pic screen_cut' id='pic"+index+"'><input type='file' id='hdnImg"+index+"' name='img"+index+"' onchange='setImagePreview("+index+")'/><img src='' class='prod_img' id='imgPreview"+index+"' width='100%' height='100%'></div>")
		}
		if($("#pic"+i).children("div").hasClass("del_but")){
			
		}else{
			$("#pic"+i).append("<div class='del_but'>删除图片</div>")
		}
	}
	return true;
	
}


//登录方法
function logindo(){
	var opetype = $("#opetype").val();
	if("0" == opetype){
		var login_name = $("#login_name").val();
		if(login_name == null || login_name == undefined || login_name == ""){
			alert("请输入登录用户账号.");
			$("#login_pwd").focus();
			return false;
		}
		var login_pwd = $("#login_pwd").val();
		if(login_pwd == null || login_pwd == undefined || login_pwd == ""){
			alert("请输入登录密码.");
			$("#login_pwd").focus();
			return false;
		}
		$.ajax({
			type: "POST",
			async: false,
			url: 'miniapp/dologin.do',
			data:{login_name: login_name, login_pwd: login_pwd, opetype: '0'},
			dataType:'json',
			success: function(data){
				var code = data.code;
				if(code == 1){
					location.reload();
				}else{
					alert(data.msg);
				}
			}
		});
		return true;
	}else{
		var email = $("#email").val();
		if(email == null || email == undefined || email == ""){
			alert("请输入邮箱.");
			$("#email").focus();
			return false;
		}
		var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
		if(!myreg.test(email)){
			alert("请输入有效的邮箱.");
			$("#email").focus();
			return false;
		}
		var name = $("#name").val();
		if(name == null || name == undefined || name == ""){
			alert("请输入账号.");
			$("#name").focus();
			return false;
		}
		var reg_pwd = $("#reg_pwd").val();
		if(reg_pwd == null || reg_pwd == undefined || reg_pwd == ""){
			alert("请输入密码.");
			$("#reg_pwd").focus();
			return false;
		}
		var sureid = $("#sureid").val();
		if(sureid == null || sureid == undefined || sureid == ""){
			alert("请输入密码确认.");
			$("#sureid").focus();
			return false;
		}
		if(reg_pwd != sureid){
			alert("两次密码输入不一致.");
			$("#reg_pwd").focus();
			return false;
		}
		var emailNum = "";
		var nameNum = "";
		//检验名称是否存在
		$.ajax({
			type: "POST",
			async: false,
			url: 'miniapp/checkuser.do?name=' + name + "&email=" + email,
			dataType:'json',
			success: function(data){
				emailNum = data.emailNum;
				nameNum = data.nameNum;
			}
		});
		if(emailNum == "" || emailNum == undefined || emailNum == null || parseInt(emailNum) != 0){
			alert("邮箱已存在.");
			return false;
		}
		if(nameNum == "" || nameNum == undefined || nameNum == null || parseInt(nameNum) != 0){
			alert("账号已存在.");
			return false;
		}
		$.ajax({
			type: "POST",
			async: false,
			url: 'miniapp/dologin.do',
			data:{email: email, name: name, reg_pwd: reg_pwd, opetype: '1'},
			dataType:'json',
			success: function(data){
				var code = data.code;
				if(code == 1){
					location.reload();
				}else{
					alert(data.msg);
				}
			}
		});
		return true;
	}
}